package acwing_05;
import java.util.*;
import java.io.*;
public class _897_线性DP_最长公共子序列_carl版 {
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static int N = 1010;
	static int n,m;
	static char a[] = new char[N];
	static char b[] = new char[N];
	static StringTokenizer st;
	static int dp[][] = new int[N][N];
	public static void main(String[] args) throws IOException {
		st = new StringTokenizer(br.readLine());
		n = Integer.parseInt(st.nextToken());
		m = Integer.parseInt(st.nextToken());
		String A = br.readLine();
		String B = br.readLine();
		for(int i = 1; i <= n; i++) {
			a[i] = A.charAt(i-1);
		}
		for(int i = 1; i <= m; i++) {
			b[i] = B.charAt(i-1);
		}

		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= m; j++) {
				// 字符从下标0开始
				if(a[i] == b[j]) {
					// 字符相同，说明需要在上一个最长子序列的基础上+1
					dp[i][j] = dp[i-1][j-1] + 1;
				} else {
					// 字符不同，需要从上方和左方取最长子序列的值
					dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
				}
			}
		}
			
		System.out.println(dp[n][m]);
 		
		
	}
}
